<?php
namespace app\controller\meals;

use app\BaseController;
use think\facade\Db;
// use app\controller\user\Index as userIndex;

/**
 * 套餐
 */
class Index extends BaseController{

    private String $total;

    public function initialize(){}

    /**
     * 获取套餐列表
     */
    public function getMealByPage(){
        
        // 获取请求里面所有数据
        $param = input("get.");
        // 每次请求开始的页面是 页数-1再乘以每页数量
        $start = ($param['start'] - 1)*$param['num'];
        // 查出分页数据
        $data = Db::name("meals")->limit($start,$param['num'])->select()->toArray();
        print_r($data);
    }

    /**
     * 加载所有套餐
     */
    public function getAllMeals(){
        // 类型加载
        $type = Db::name('meal_type')->orderRaw('rank')->select()->toArray();
        $meals = Db::name('meals')->field('id,name,img,money,origin,meal_type_id')->select()->toArray();
        $list = [];
        // 组合数据为前端vtabs需要的格式 [{title:'title',children:[{...}]},...]
        foreach($type as $key=>$value){
            $children = [];
            $i = 0;
            $list[$key]['title'] = $value['name'];
            $list[$key]['tip']  = $value['tip'];
            $list[$key]['limitDate']  = $value['start_date'].'至'.$value['end_date'];
            foreach($meals as $k=>$v){
                if ($value['id'] == $v['meal_type_id']) {
                    $i ++;
                    $children[$i]['id'] = $v['id'];
                    $children[$i]['name'] = $v['name'];
                    $children[$i]['price'] = $v['money'];
                    $children[$i]['origin'] = $v['origin'];
                    $children[$i]['img'] = explode(",,",$v['img']);
                    $list[$key]['children'] = $children;
                }
            }
        }
        $userIndex = new \app\controller\user\Index($this->app);
        $yuyueCount = $userIndex->getYuyueCount();
        return BaseController::returnSuccessMsg('10001',['list'=>$list,'amount'=>$yuyueCount]);
    }


    /**
     * 积分微商城列表
     */
    public function loadIntegral(){
        $param = input("get.");
        // 每次请求开始的页面是 页数-1再乘以每页数量
        $start = ($param['start'] - 1)*$param['num'];
        $count = Db::name('integral')
        ->count();
        // if ($start*$param['num'] >= $count) {
        //     return BaseController::returnSuccessMsg('10001',-1);
        // }
        $list = Db::name('integral')
        ->limit($start,$param['num'])
        ->select();

        return BaseController::returnSuccessMsg('10001',['list'=>$list,'total'=>$count]);
    }




    

}

?>